package org.gjy.m8.concurrent;

import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-07-03 15:03:10
 */
@Data
public class DbRunnable implements Runnable {
    private static final Logger log = LoggerFactory.getLogger(DbRunnable.class);
    private Integer id;
    private Semaphore semaphore;

    public DbRunnable(Integer id) {
        this.id = id;
    }

    public DbRunnable(Integer id, Semaphore semaphore) {
        this.id = id;
        this.semaphore = semaphore;
    }

    @Override
    public void run() {
        try {
            log.info("处理订单编码: {} 开始", id);
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            log.info("处理订单编码: {} 完成", id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            semaphore.release();
        }
    }

}
